**************************************************************************************************************************************************
****************************** Wealth-Income Ratios in Free Market Capitalism: Switzerland, 1900-2020 ********************************************
**************************************************************************************************************************************************

clear all
set more off
set scheme s1color  

**Author: Enea Baselgia and Isabel Z. Martinez
**Date: January, 25 2023

*** set this path to the current directory
*** global mypath "C:\Users\EBaselgia\Dropbox\WIR_project\Publication_process\replication"

**Install package to download data directly from WID.world
/* 
ssc install wid

** The documentation of the database is available after installing the package via:
help wid

** See also: https://wid.world/codes-dictionary/

*/

*** TO DOWNLOAD THE RAW DATA FROM WID.WORLD, RUN THE CODE FROM LINE 32 TO 282
*** THIS WILL DOWNLOAD AND SAVE THE COUNTRY FILES (E.G. FOR GERMANY: "DE_WID.dta") 
*** THE DATA IN THE FOLDER "raw_data/wid_world" WERE DOWNLOADED ON AUGUST 18, 2022 (NOTE THAT WID.WORLD REGULARLY UPDATES ITS DATABASE)



***** Preparation of the data set *****
/*
** We download the data for: DE=Germany, FR=France, IT=Italy, GB=United Kingdom, SE=Sweden, US=United States, JP=Japan, NO=Norway, AU=Austrailia, CA=Canada
** Clean and prepare each individual dataset and merge it into a single dataset that contains all the necessary international (WID.World) data


local ctry DE FR ES IT GB SE US JP NO CA

foreach z of local ctry {
	
	wid, indicators(_all) areas(`z') ages(999) year(1900/2020) perc(p0p100) pop(i) clear

drop country percentile age pop

***** transform the data into wide format and rename variables
reshape wide value, i(year) j(variable) string
 foreach var of varlist value* {
   	local newname : subinstr local var "`z'" ""
   	rename `var' `newname'
 }

foreach x of var * { 
	rename `x' `z'_`x' 
} 
rename `z'_year year



*** National Income (NNI)
gen `z'_nni_T_R = `z'_valuemnninc999i
label variable `z'_nni_T_R "National Income (nninc) - `z'"

***Private Wealth and PWIR
gen `z'_domW_agriW_T_R = `z'_valuempwbus999i
label variable `z'_domW_agriW_T_R "Other non-financial assets (pwbus) - `z'"
gen `z'_domW_agri_WIR = `z'_domW_agriW_T_R / `z'_nni_T_R
label variable `z'_domW_agri_WIR "Other non-financial assets in % NNI - `z'"

gen `z'_pW_T_R = `z'_valuempweal999i
label variable `z'_pW_T_R "Net Private Wealth - `z'"
gen `z'_pW_T_R_excl_domW_agriW = `z'_pW_T_R - `z'_domW_agriW_T_R
label variable `z'_pW_T_R_excl_domW_agriW "Net Private Wealth excl. Other non-financial assets - `z'"

gen `z'_pWIR = `z'_pW_T_R / `z'_nni_T_R
label variable `z'_pWIR "Net Private Wealth Income Ratio"
gen `z'_pWIR_excl_domW_agriW = `z'_pW_T_R_excl_domW_agriW / `z'_nni_T_R
label variable `z'_pWIR_excl_domW_agriW "Net Private Wealth Income Ratio excl. Other non-financial assets - `z'"

gen `z'_pW_housing_T_R = `z'_valuempwhou999i
label variable `z'_pW_housing_T_R "Housing Wealth (pwhou) - `z'"
gen `z'_pWIR_housing = `z'_pW_housing_T_R / `z'_nni_T_R
label variable `z'_pWIR_housing "Private Housing Wealth-Income Ratio - `z'"

*** Public Wealth
gen `z'_gW_T_R = `z'_valuemgweal999i
label variable `z'_gW_T_R "Net Government Wealth (gweal) - `z'"
gen `z'_gWIR = `z'_gW_T_R / `z'_nni_T_R
label variable `z'_gWIR "Government Wealth Income Ratio - `z'"

*** National Wealth and components
gen `z'_nW_T_R = `z'_pW_T_R + `z'_gW_T_R
label variable `z'_nW_T_R "Net National Wealth (nweal) - `z'"
gen `z'_nWIR = `z'_nW_T_R / `z'_nni_T_R
label variable `z'_nWIR "National Wealth-Income Ratio - `z'"
gen `z'_nWIR_excl_domW_agriW = `z'_pWIR_excl_domW_agriW + `z'_gWIR
label variable `z'_nWIR_excl_domW_agriW "Net National Wealth excl. Other non-financial assets - `z'"

*** Net Foreign Wealth 
gen `z'_nfaW_T_R = `z'_valuemnwnxa999i
label variable `z'_nfaW_T_R "Net Foreign Wealth (nwnxa) - `z'"
gen `z'_nfaWIR = `z'_nfaW_T_R / `z'_nni_T_R
label variable `z'_nfaWIR "Net Foreign Wealth-Income Ratio - `z'"


***** Savings Rates *****
gen `z'_saveR_corp_nonfin = `z'_valuemsecnf999i / `z'_nni_T_R
label variable `z'_saveR_corp_nonfin "non-financial corporate savings in % NNI - `z'"

gen `z'_saveR_corp_fin = `z'_valuemsecfc999i / `z'_nni_T_R
label variable `z'_saveR_corp_fin "financial corporate savings in % NNI - `z'"

gen `z'_saveR_corp = `z'_valuemsecco999i / `z'_nni_T_R
label variable `z'_saveR_corp "corporate savings in % NNI - `z'"

gen `z'_saveR_HH = `z'_valuemsavho999i / `z'_nni_T_R
label variable `z'_saveR_HH "household savings in % NNI - `z'"

gen `z'_saveR_nP = `z'_valuemsavnp999i / `z'_nni_T_R
label variable `z'_saveR_nP "NPISH savings in % NNI - `z'"

gen `z'_saveR_n = `z'_valuemsavin999i / `z'_nni_T_R
label variable `z'_saveR_n "national savings in % NNI - `z'"

gen `z'_saveR_g = `z'_valuemsavgo999i / `z'_nni_T_R
label variable `z'_saveR_g "public savings in % NNI - `z'"

gen `z'_saveR_p = `z'_saveR_n - `z'_saveR_g
label variable `z'_saveR_p "private savings in % NNI - `z'"

gen `z'_saveR_HH_nP = `z'_saveR_p - `z'_saveR_corp
label variable `z'_saveR_HH_nP "household and NPISH savings in % NNI - `z'"

***** Population *****
gen `z'_pop_total = `z'_valuenpopul999i
label variable `z'_pop_total "total population - `z'"
gen `z'_pop_gr = `z'_pop_total / `z'_pop_total[_n-1] -1
label variable `z'_pop_gr "population growth rate - `z'"

***** National Income *****
gen `z'_nni_PI = `z'_valueinyixx999i
label variable `z'_nni_PI "National Income Price In`z'x - `z'"
gen `z'_nni_PC_R = `z'_nni_T_R / `z'_pop_total
label variable `z'_nni_PC_R "Real Net National Income per capita - `z'"

gen `z'_nni_gr_T_R = `z'_nni_T_R / `z'_nni_T_R[_n-1] -1
label variable `z'_nni_gr_T_R "Real Growth of Net National Income - `z'"
gen `z'_nni_gr_PC_R = `z'_nni_PC_R / `z'_nni_PC_R[_n-1] -1
label variable `z'_nni_gr_PC_R "Real Growth of per capita Net National Income - `z'"

gen `z'_gdp = `z'_valuemgdpro999i
gen `z'_ndp = `z'_valuemndpro999i
label variable `z'_gdp "Gross Domestic Product - `z'"
label variable `z'_ndp "Net Domestic Product - `z'"

***** drop variables 
keep year `z'_saveR_p `z'_saveR_nP `z'_saveR_n `z'_saveR_g `z'_saveR_corp_nonfin `z'_saveR_corp_fin `z'_saveR_corp `z'_saveR_HH_nP `z'_saveR_HH `z'_pop_total `z'_pop_gr `z'_pW_housing_T_R `z'_pW_T_R_excl_domW_agriW `z'_pW_T_R `z'_pWIR_housing `z'_pWIR_excl_domW_agriW `z'_pWIR `z'_nni_gr_T_R `z'_nni_gr_PC_R `z'_nni_T_R `z'_nni_PI `z'_nni_PC_R `z'_nfaW_T_R `z'_nfaWIR `z'_ndp `z'_nW_T_R `z'_nWIR_excl_domW_agriW `z'_nWIR `z'_gdp `z'_gW_T_R `z'_gWIR `z'_domW_agri_WIR `z'_domW_agriW_T_R

***** save data 
cd "$mypath/raw_data/"
save "`z'_WID.dta", replace

}



***For Australia some variables are missing (run this code to download the data)
wid, indicators(_all) areas(AU) ages(999) year(1900/2020) perc(p0p100) pop(i) clear

drop country percentile age pop

***** transform the data into wide format and rename variables
reshape wide value, i(year) j(variable) string
 foreach var of varlist value* {
   	local newname : subinstr local var "AU" ""
   	rename `var' `newname'
 }

foreach x of var * { 
	rename `x' AU_`x' 
} 
rename AU_year year



*** National Income (NNI)
gen AU_nni_T_R = AU_valuemnninc999i
label variable AU_nni_T_R "National Income (nninc) - AU"

***Private Wealth and PWIR
gen AU_domW_agriW_T_R = AU_valuempwbus999i
label variable AU_domW_agriW_T_R "Other non-financial assets (pwbus) - AU"
gen AU_domW_agri_WIR = AU_domW_agriW_T_R / AU_nni_T_R
label variable AU_domW_agri_WIR "Other non-financial assets in % NNI - AU"

gen AU_pW_T_R = AU_valuempweal999i
label variable AU_pW_T_R "Net Private Wealth - AU"
gen AU_pW_T_R_excl_domW_agriW = AU_pW_T_R - AU_domW_agriW_T_R
label variable AU_pW_T_R_excl_domW_agriW "Net Private Wealth excl. Other non-financial assets - AU"

gen AU_pWIR = AU_pW_T_R / AU_nni_T_R
label variable AU_pWIR "Net Private Wealth Income Ratio"
gen AU_pWIR_excl_domW_agriW = AU_pW_T_R_excl_domW_agriW / AU_nni_T_R
label variable AU_pWIR_excl_domW_agriW "Net Private Wealth Income Ratio excl. Other non-financial assets - AU"

gen AU_pW_housing_T_R = AU_valuempwhou999i
label variable AU_pW_housing_T_R "Housing Wealth (pwhou) - AU"
gen AU_pWIR_housing = AU_pW_housing_T_R / AU_nni_T_R
label variable AU_pWIR_housing "Private Housing Wealth-Income Ratio - AU"

*** Public Wealth
gen AU_gW_T_R = AU_valuemgweal999i
label variable AU_gW_T_R "Net Government Wealth (gweal) - AU"
gen AU_gWIR = AU_gW_T_R / AU_nni_T_R
label variable AU_gWIR "Government Wealth Income Ratio - AU"

*** National Wealth and components
gen AU_nW_T_R = AU_pW_T_R + AU_gW_T_R
label variable AU_nW_T_R "Net National Wealth (nweal) - AU"
gen AU_nWIR = AU_nW_T_R / AU_nni_T_R
label variable AU_nWIR "National Wealth-Income Ratio - AU"
gen AU_nWIR_excl_domW_agriW = AU_pWIR_excl_domW_agriW + AU_gWIR
label variable AU_nWIR_excl_domW_agriW "Net National Wealth excl. Other non-financial assets - AU"

*** Net Foreign Wealth 
gen AU_nfaW_T_R = AU_valuemnwnxa999i
label variable AU_nfaW_T_R "Net Foreign Wealth (nwnxa) - AU"
gen AU_nfaWIR = AU_nfaW_T_R / AU_nni_T_R
label variable AU_nfaWIR "Net Foreign Wealth-Income Ratio - AU"


***** Savings Rates *****
gen AU_saveR_corp_nonfin = AU_valuemsecnf999i / AU_nni_T_R
label variable AU_saveR_corp_nonfin "non-financial corporate savings in % NNI - AU"

gen AU_saveR_corp_fin = AU_valuemsecfc999i / AU_nni_T_R
label variable AU_saveR_corp_fin "financial corporate savings in % NNI - AU"

gen AU_saveR_corp = AU_valuemsecco999i / AU_nni_T_R
label variable AU_saveR_corp "corporate savings in % NNI - AU"

gen AU_saveR_n = AU_valuemsavin999i / AU_nni_T_R
label variable AU_saveR_n "national savings in % NNI - AU"

gen AU_saveR_g = AU_valuemsavgo999i / AU_nni_T_R
label variable AU_saveR_g "public savings in % NNI - AU"

gen AU_saveR_p = AU_saveR_n - AU_saveR_g
label variable AU_saveR_p "private savings in % NNI - AU"

gen AU_saveR_HH_nP = AU_saveR_p - AU_saveR_corp
label variable AU_saveR_HH_nP "household and NPISH savings in % NNI - AU"

***** Population *****
gen AU_pop_total = AU_valuenpopul999i
label variable AU_pop_total "total population - AU"
gen AU_pop_gr = AU_pop_total / AU_pop_total[_n-1] -1
label variable AU_pop_gr "population growth rate - AU"

***** National Income *****
gen AU_nni_PI = AU_valueinyixx999i
label variable AU_nni_PI "National Income Price InAUx - AU"
gen AU_nni_PC_R = AU_nni_T_R / AU_pop_total
label variable AU_nni_PC_R "Real Net National Income per capita - AU"

gen AU_nni_gr_T_R = AU_nni_T_R / AU_nni_T_R[_n-1] -1
label variable AU_nni_gr_T_R "Real Growth of Net National Income - AU"
gen AU_nni_gr_PC_R = AU_nni_PC_R / AU_nni_PC_R[_n-1] -1
label variable AU_nni_gr_PC_R "Real Growth of per capita Net National Income - AU"

gen AU_gdp = AU_valuemgdpro999i
gen AU_ndp = AU_valuemndpro999i
label variable AU_gdp "Gross Domestic Product - AU"
label variable AU_ndp "Net Domestic Product - AU"

***** drop variables 
keep year AU_saveR_p AU_saveR_n AU_saveR_g AU_saveR_corp_nonfin AU_saveR_corp_fin AU_saveR_corp AU_saveR_HH_nP  AU_pop_total AU_pop_gr AU_pW_housing_T_R AU_pW_T_R_excl_domW_agriW AU_pW_T_R AU_pWIR_housing AU_pWIR_excl_domW_agriW AU_pWIR AU_nni_gr_T_R AU_nni_gr_PC_R AU_nni_T_R AU_nni_PI AU_nni_PC_R AU_nfaW_T_R AU_nfaWIR AU_ndp AU_nW_T_R AU_nWIR_excl_domW_agriW AU_nWIR AU_gdp AU_gW_T_R AU_gWIR AU_domW_agri_WIR AU_domW_agriW_T_R

***** save data 
cd "$mypath/raw_data/"
save "AU_WID.dta", replace
*/

*****MERGE COUNTRY DATA-SETS***** 
clear all
cd "$mypath/raw_data/wid_world/"
use "AU_WID.dta", clear

local ctry DE FR ES IT GB SE US JP NO CA

foreach z of local ctry {	

merge 1:1 year using "`z'_WID.dta"
drop _merge
	
	} 
	
save "WID.dta", replace

cd "$mypath/"
   